#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 255;

int n, m;
int a[N];
int dp[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) rep(j, 1, n) dp[i][j] = -10 - i;  // make them diff
  rep(i, 1, n) dp[i][i] = a[i];
  int ans = *max_element(a + 1, a + n + 1);
  rep(len, 2, n) {
    rep(l, 1, n + 1 - len) {
      int r = l + len - 1;
      rep(i, l, r - 1) {
        if (dp[l][i] == dp[i + 1][r]) {
          dp[l][r] = dp[l][i] + 1;
          ans = max(ans, dp[l][r]);
        }
      }
    }
  }
  cout << ans;
  return 0;
}